package cn.com.eado.crm.vm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import cn.com.eado.crm.app.utils.LogUtil
import cn.com.eado.crm.m.bean.PostCompany
import cn.com.eado.crm.m.db.Company
import cn.com.eado.crm.m.repository.CompanyRepository
import cn.com.eado.crm.model.AbsentLiveData
import cn.com.eado.crm.model.Resource

/**
 *
 */
class CompanyViewModel : ViewModel() {
    private val repository by lazy { CompanyRepository() }

    private val _loadCompany = MediatorLiveData<Boolean>()  //可观察的
    private val loadCompany: LiveData<Boolean> = _loadCompany
    //http请求参数
    var companyName: String? = null
    var groupName: String? = null
    var industryName: String? = null
    var linkName: String? = null
    var address: String? = null
    var areaID: String? = null
    var userName: String? = null
//    var userId = 0
    var page = 1
    var rows = 20

    //指定请求返回的类型为:String（本app的所有请求基本使用此方式）
    val company: LiveData<Resource<Company>> = Transformations.switchMap(loadCompany){ load ->
        if (load){
            LogUtil.e("tag", "load Company: companyName = $companyName, groupName = $groupName, " +
                    "industryName = $industryName, linkName = $linkName, address = $address, " +
                    "areaID = $areaID, userName = $userName, rows=$rows, page=$page")
            repository.loadCompany(companyName, groupName, industryName, linkName, address, areaID, userName, rows, page)
        }else{
            AbsentLiveData.create()  //null
        }
    }

    fun setLoadCompany(b: Boolean){
        _loadCompany.value = b  //赋值时，会触发loginResult的执行。true:则执行repository.login(name, pwd, mac)
    }

    fun refresh(){
        page = 1
        _loadCompany.value?.let{ _loadCompany.value = it }
    }

    fun loadNextPage(){
        _loadCompany.value?.let{ _loadCompany.value = it }
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    var t: PostCompany? = null
    private val _post = MediatorLiveData<Boolean>()
    private val post: LiveData<Boolean> = _post

    val postCompany: LiveData<Resource<String>> = Transformations.switchMap(post){ post ->
        if (post){
//            LogUtil.e("tag", "postCompany: userId = $userId, data=$t")
            repository.saveCompany(1, t!!)
        }else{
            AbsentLiveData.create()  //null
        }
    }

    fun setSave(b: Boolean){
        _post.value = b  //赋值时，会触发loginResult的执行。true:则执行repository.login(name, pwd, mac)
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    //todo
//    private val _search = MediatorLiveData<Boolean>()  //可观察的
//    private val search: LiveData<Boolean> = _search
//    var page2 = 1
////    var companyName = ""
//
//    val searchCompany: LiveData<Resource<Company>> = Transformations.switchMap(search){ search ->
//        if (search){
////            LogUtil.e("tag", "postCompany: userId = $userId, data=$t")
//            repository.searchCompany(1, rows, page2, "")
//        }else{
//            AbsentLiveData.create()  //null
//        }
//    }
//
//    fun setSearch(b: Boolean){
//        _search.value = b  //赋值时，会触发loginResult的执行。true:则执行repository.login(name, pwd, mac)
//    }

}